package com.beaven.model;

import android.content.Context;
import android.support.annotation.StringRes;
import com.beaven.model.data.DataSource;
import com.beaven.model.data.group.GroupCenter;
import com.beaven.model.data.group.GroupDispatcher;
import com.beaven.model.data.message.MessageCenter;
import com.beaven.model.data.message.MessageDispatcher;
import com.beaven.model.data.user.UserCenter;
import com.beaven.model.data.user.UserDispatcher;
import com.beaven.model.model.api.RspModel;
import com.beaven.model.model.base.BaseApplication;
import com.beaven.model.model.sp.Account;
import com.beaven.model.utils.DBFlowExclusionStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowManager;

/**
 * author: Beaven
 * time: 2017/5/18 10:50
 */

public class Factory {

  private static Gson gson = null;

  public static void init() {
    // 数据库初始化
    FlowManager.init(new FlowConfig.Builder(getApplication()).openDatabasesOnInit(true).build());
    //  本地数据SP加载
    Account.loadFromLocal(getApplication());
  }

  public static Gson getGson() {
    if (gson == null) {
      gson = new GsonBuilder()
          // 设置时间格式
          .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
          // 设置一个过滤器，数据库级别的Model不进行Json转换
          .setExclusionStrategies(new DBFlowExclusionStrategy()).create();
    }
    return gson;
  }

  public static Context getApplication() {
    return BaseApplication.getInstance();
  }

  // 网络请求定义状态解析
  public static void decodeRspCode(RspModel model, DataSource.FailedCallBack callback) {
    if (model == null) return;

    switch (model.getCode()) {
      case RspModel.SUCCEED:
        break;
      case RspModel.ERROR_SERVICE:
        decodeRspCode(R.string.data_rsp_error_service, callback);
        break;
      case RspModel.ERROR_NOT_FOUND_USER:
        decodeRspCode(R.string.data_rsp_error_not_found_user, callback);
        break;
      case RspModel.ERROR_NOT_FOUND_GROUP:
        decodeRspCode(R.string.data_rsp_error_not_found_group, callback);
        break;
      case RspModel.ERROR_NOT_FOUND_GROUP_MEMBER:
        decodeRspCode(R.string.data_rsp_error_not_found_group_member, callback);
        break;
      case RspModel.ERROR_CREATE_USER:
        decodeRspCode(R.string.data_rsp_error_create_user, callback);
        break;
      case RspModel.ERROR_CREATE_GROUP:
        decodeRspCode(R.string.data_rsp_error_create_group, callback);
        break;
      case RspModel.ERROR_CREATE_MESSAGE:
        decodeRspCode(R.string.data_rsp_error_create_message, callback);
        break;
      case RspModel.ERROR_PARAMETERS:
        decodeRspCode(R.string.data_rsp_error_parameters, callback);
        break;
      case RspModel.ERROR_PARAMETERS_EXIST_ACCOUNT:
        decodeRspCode(R.string.data_rsp_error_parameters_exist_account, callback);
        break;
      case RspModel.ERROR_PARAMETERS_EXIST_NAME:
        decodeRspCode(R.string.data_rsp_error_parameters_exist_name, callback);
        break;
      case RspModel.ERROR_ACCOUNT_TOKEN:
        break;
      case RspModel.ERROR_ACCOUNT_LOGIN:
        decodeRspCode(R.string.data_rsp_error_account_login, callback);
        break;
      case RspModel.ERROR_ACCOUNT_REGISTER:
        decodeRspCode(R.string.data_rsp_error_account_register, callback);
        break;
      case RspModel.ERROR_ACCOUNT_NO_PERMISSION:
        decodeRspCode(R.string.data_rsp_error_account_no_permission, callback);
        break;
      case RspModel.ERROR_UNKNOWN:
      default:
        decodeRspCode(R.string.data_rsp_error_unknown, callback);
        break;
    }
  }

  private static void decodeRspCode(@StringRes final int resId,
      final DataSource.FailedCallBack callback) {
    if (callback != null) {
      callback.loadDataFail(resId);
    }
  }

  public static UserCenter getUserCenter() {
    return UserDispatcher.getInstance();
  }

  public static MessageCenter getMessageCenter() {
    return MessageDispatcher.getInstance();
  }

  public static GroupCenter getGroupCenter() {
    return GroupDispatcher.getInstance();
  }

}
